package com.prp.gwt.client.widgets.register;

import java.util.ArrayList;
import java.util.Map;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.register.RegisterClientListener;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.widgets.MessageDialogBox;

public class RegisterForm extends VerticalPanel{

	public static RegisterForm instance;
	private FlexTable table = new FlexTable();
	private TextBox email = new TextBox();
	private TextBox username = new TextBox();
	private PasswordTextBox password = new PasswordTextBox();
	private PasswordTextBox passwordConfirm = new PasswordTextBox();
	private TextBox realname = new TextBox();
	private TextArea agreement = new TextArea();
	private CheckBox checkBox = new CheckBox("同意条款");
	private ListBox ageBox = new ListBox();
	private RadioButton male = new RadioButton("gender", "男");
	private RadioButton female = new RadioButton("gender", "女");
	HorizontalPanel checkPanel = new HorizontalPanel();
	private Button commit = new Button("下一步");
	private Button reset = new Button("重置");
	private HTML message = new HTML("");
	private ChangeHandler handler = new myChangeHandler();
	private ArrayList<TextBoxBase> boxes = new ArrayList<TextBoxBase>();
	
	public static RegisterForm getInstance() {
		if (instance == null)
			new RegisterForm();
		return instance;
	}

	private RegisterForm() {
		instance = this;
		add(initTable());
		add(getGender());

		add(getAge());

		agreement.setCharacterWidth(80);
		agreement.setVisibleLines(13);
		agreement.setReadOnly(true);
		getAgreement();
		add(agreement);

		checkPanel.add(checkBox);
		checkPanel.add(message);
		checkPanel.setSpacing(10);
		checkBox.setValue(true);
		add(checkPanel);

		add(getBottom());
		setSpacing(10);

		commit.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if(!check())
					return;
				User user = new User();
				user.setEmail(email.getText());
				user.setUsername(username.getText());
				user.setAge(ageBox.getSelectedIndex() + 1);
				user.setGender(male.getValue());
				user.setRealname(realname.getText());
				RegisterClientListener.getInstance().register(user, password.getText());
			}
		});

		reset.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				reset();
			}
		});
	}

	private boolean check() {
		if(email.getValue() == null || email.getValue().equals("")){
			table.setText(0, 2, "邮箱不能为空");
			return false;
		}
		if(username.getValue() == null || username.equals("")){
			table.setText(1, 2, "用户名不能为空");
			return false;
		}
		if (password.getValue() == null || password.getValue().equals("")) {
			table.setText(2, 2, "密码不能为空");
			return false;
		}
		if (!password.getValue().equals(passwordConfirm.getValue())) {
			table.setText(2, 2, "请确认密码");
			return false;
		}
		if (!checkBox.getValue()) {
			message.setText("您未同意条款");
			return false;
		}

		return true;
	}

	public void reset() {
		for (TextBoxBase box : boxes)
			box.setText("");
	}

	public void hideAgreement() {
		remove(agreement);
		remove(checkPanel);
	}

	private FlexTable initTable() {
		table.setText(0, 0, "*邮箱:");
		setWidget(0, 1, email);
		table.setText(0, 2, "");
		table.setText(1, 0, "*用户名:");
		setWidget(1, 1, username);
		table.setText(1, 2, "(4-16个字符)");
		table.setText(1, 3, "");
		table.setText(2, 0, "*密码:");
		setWidget(2, 1, password);
		table.setText(2, 2, "");
		table.setText(3, 0, "*密码确认:");
		setWidget(3, 1, passwordConfirm);
		table.setText(3, 2, "");
		table.setText(4, 0, "真实姓名:");
		setWidget(4, 1, realname);
		table.setText(4, 2, "");
		table.setCellSpacing(5);
		return table;
	}

	public void getAgreement() {
		String url = Host.getAGREEMENT();
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL
				.encode(url));
		builder.setCallback(new RequestCallback() {
			@Override
			public void onError(Request request, Throwable exception) {
				MessageDialogBox.show("对不起", "出错了,请稍后再试");
			}

			@Override
			public void onResponseReceived(Request request, Response response) {
				agreement.setText(response.getText());
			}
		});
		try {
			builder.send();
		} catch (RequestException e) {
			e.printStackTrace();
		}
	}

	public void setWidget(int row, int column, Widget widget) {
		table.setWidget(row, column, widget);
		if (widget instanceof FocusWidget)
			((FocusWidget) widget).setFocus(true);
		if (widget instanceof TextBoxBase) {
			((TextBoxBase) widget).selectAll();
			((TextBoxBase) widget).addChangeHandler(handler);
			boxes.add((TextBoxBase) widget);
		}

	}

	private void clean(Widget source) {
		int row = boxes.indexOf(source);
		if (row >= 0)
			table.setText(row, table.getCellCount(row) - 1, "");
	}

	class myChangeHandler implements ChangeHandler {
		@Override
		public void onChange(ChangeEvent event) {
			clean((Widget) event.getSource());
		}
	}

	public void showErr(Map<Integer, String> result) {
		for (Integer n : result.keySet()) {
			HTML err = new HTML(result.get(n));
			setWidget(n, 2, err);
		}
	}

	private HorizontalPanel getGender() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(3);
		panel.add(new HTML("性别:"));
		male.setValue(true);
		panel.add(male);
		panel.add(female);
		return panel;
	}

	private HorizontalPanel getAge() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(3);
		panel.add(new HTML("年龄:"));
		for (int i = 1; i < 31; i++)
			ageBox.addItem(String.valueOf(i));
		ageBox.setSelectedIndex(17);
		panel.add(ageBox);
		return panel;
	}

	private HorizontalPanel getBottom() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(commit);
		panel.add(reset);
		panel.setSpacing(3);
		return panel;
	}

}
